package com.fau.wm.entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.hibernate.Hibernate;
import org.hibernate.annotations.GenericGenerator;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

/**
 * 出库详细表
 * @author liy
 * @since 2014年7月24日,下午6:52:52
 */
@Entity
@Table(name = "medical_recall_detail")
public class MedicalRecallDetail implements Serializable, JSONAware {

	private static final long serialVersionUID = 3966303335813655694L;
	
	private String id;
	private String medicineId;			// 药品id
	private String recallId;			// 出库单id
	private Integer quantity;			// 数量
	private Date createTime = new Date();
	private Date updateTime = new Date();
	
	private String warehouseId;			// 仓库id
	private MedicalMedicine medicine;	// 药品实体
	private String medicineName;        // 药品名称
	private MedicalRecall recall;		// 出库单实体
	private String warehouseName;       // 仓库名称
	private String hospitalId;			// 医药企业id
	
	private String serialNum;
	private String medicineSerialNum;
	private String medicineUnit;
	private String title;
	private Date startTime;
	private Date endTime;
	private String rType;				// 出库类型
	
	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "id", length = 32)
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	@Transient
	public String getMedicineUnit() {
		return medicineUnit;
	}

	public void setMedicineUnit(String medicineUnit) {
		this.medicineUnit = medicineUnit;
	}

	@Transient
	public String getMedicineId() {
		return medicineId;
	}

	public void setMedicineId(String medicineId) {
		this.medicineId = medicineId;
	}

	@Transient
	public String getRecallId() {
		return recallId;
	}

	public void setRecallId(String recallId) {
		this.recallId = recallId;
	}

	@Column(name = "quantity", length = 11)
	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	@Column(name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "update_time")
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	
	@Transient
	public String getWarehouseId() {
		return warehouseId;
	}

	public void setWarehouseId(String warehouseId) {
		this.warehouseId = warehouseId;
	}
	
	@Transient
	public String getMedicineName() {
		return medicineName;
	}

	public void setMedicineName(String medicineName) {
		this.medicineName = medicineName;
	}
	
	@Transient
	public String getWarehouseName() {
		return warehouseName;
	}

	public void setWarehouseName(String warehouseName) {
		this.warehouseName = warehouseName;
	}
	
	@Transient
	public String getHospitalId() {
		return hospitalId;
	}

	public void setHospitalId(String hospitalId) {
		this.hospitalId = hospitalId;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "medicine_id")
	public MedicalMedicine getMedicine() {
		return medicine;
	}

	public void setMedicine(MedicalMedicine medicine) {
		this.medicine = medicine;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "recall_id")
	public MedicalRecall getRecall() {
		return recall;
	}

	public void setRecall(MedicalRecall recall) {
		this.recall = recall;
	}

	@Transient
	public String getSerialNum() {
		return serialNum;
	}

	public void setSerialNum(String serialNum) {
		this.serialNum = serialNum;
	}

	@Transient
	public String getMedicineSerialNum() {
		return medicineSerialNum;
	}

	public void setMedicineSerialNum(String medicineSerialNum) {
		this.medicineSerialNum = medicineSerialNum;
	}

	@Transient
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Transient
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	@Transient
	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	
	@Transient
	public String getrType() {
		return rType;
	}

	public void setrType(String rType) {
		this.rType = rType;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("medicineId", medicine.getId());
		obj.put("medicineUnit", medicineUnit);
		obj.put("medicineName", medicine.getName());
		obj.put("medicineSerialNum", medicine.getSerialNum());
		if(Hibernate.isInitialized(recall)){
			obj.put("recallId", recall.getId());
			obj.put("serialNum", recall.getSerialNum());
			obj.put("title", recall.getTitle());
			obj.put("recallTime",DateFormatUtils.format(recall.getRecallTime(),"yyyy-MM-dd"));
			obj.put("type", recall.getType());
			obj.put("warehouse", recall.getWarehouse().getName());
			obj.put("warehouseId", recall.getWarehouse().getId());
		}
		obj.put("quantity", quantity);
		obj.put("createTime", DateFormatUtils.format(createTime, "yyyy-MM-dd HH:mm:ss"));
		obj.put("rType", rType);
		return obj.toString();
	}
	
}
